package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.entity.*;
import com.company.project.mapper.EXzqhMapper;
import com.company.project.mapper.VaccinationMapper;
import com.company.project.service.HttpSessionService;
import com.company.project.service.VaccinationStatisticsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.company.project.common.utils.DataResult;

import com.company.project.service.VaccinationService;

import javax.annotation.Resource;


/**
 * ${comments}
 *
 * @author liuhui
 * @email *****@mail.com
 * @date 2021-03-25 23:06:48
 */
@Controller
@RequestMapping("/")
public class VaccinationController {
    @Autowired
    private VaccinationService vaccinationService;

    @Autowired
    private VaccinationStatisticsService vaccinationStatisticsService;
    @Resource
    private HttpSessionService httpSessionService;
    @Resource
    private EXzqhMapper eXzqhMapper;
    @Resource
    private VaccinationMapper vaccinationMapper;

    /**
     * 跳转到页面
     */
    @GetMapping("/index/vaccination")
    public String vaccination() {
        return "vaccination/list";
    }

    /**
     * 跳转到页面
     */
    @GetMapping("/index/vaccinationlist")
    public String vaccinationlist() {
        return "vaccination/vaccinationlist";
    }

    @ApiOperation(value = "新增")
    @PostMapping("vaccination/add")
    @RequiresPermissions("vaccination:add")
    @ResponseBody
    public DataResult add(@RequestBody VaccinationEntity vaccination){
        if (vaccination.getCreateTime() == null) {
            vaccination.setCreateTime(new Date());
        }
        if (vaccination.getUpdateTime() == null) {
            vaccination.setUpdateTime(new Date());
        }
        if (StringUtils.isBlank(vaccination.getXzqh())) {
            vaccination.setXzqh(vaccination.getDepartment());
        }
        vaccination.setSysUserId(httpSessionService.getCurrentUserId());
        vaccinationService.save(vaccination);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("vaccination/delete")
    @RequiresPermissions("vaccination:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        vaccinationService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("vaccination/update")
    @RequiresPermissions("vaccination:update")
    @ResponseBody
    public DataResult update(@RequestBody VaccinationEntity vaccination){
        vaccinationService.updateById(vaccination);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("vaccination/listByPage")
    @RequiresPermissions("vaccination:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody VaccinationEntity vaccination){
        Page page = new Page(vaccination.getPage(), vaccination.getLimit());
        LambdaQueryWrapper<VaccinationEntity> queryWrapper = Wrappers.lambdaQuery();
        //姓名
        if (!org.springframework.util.StringUtils.isEmpty(vaccination.getName())) {
            queryWrapper.like(VaccinationEntity::getName, vaccination.getName());
        }
        //行政区
        if (!org.springframework.util.StringUtils.isEmpty(vaccination.getXzq())) {
            String xzq = vaccination.getXzq();
            EXzqhEntity eXzqhEntity = eXzqhMapper.selectOne(Wrappers.<EXzqhEntity>lambdaQuery().eq(EXzqhEntity::getMC,  xzq));
            String xaqh = eXzqhEntity.getDM().substring(0,9);
            queryWrapper.like(VaccinationEntity::getXzqh, xaqh);
        }
        //身份证号
        if (!org.springframework.util.StringUtils.isEmpty(vaccination.getIdentity())) {
            queryWrapper.like(VaccinationEntity::getIdentity, vaccination.getIdentity());
        }
        //开始日期
        if (!org.springframework.util.StringUtils.isEmpty(vaccination.getStartTime())) {

            queryWrapper.apply("trunc(create_time) >= "+"to_date('"+vaccination.getStartTime().trim().substring(0,10)+"','yyyy-mm-dd')");
        }
        //结束日期
        if (!org.springframework.util.StringUtils.isEmpty(vaccination.getEndTime())) {
            queryWrapper.apply("trunc(create_time) <= "+"to_date('"+vaccination.getEndTime().trim().substring(0,10)+"','yyyy-mm-dd')");
        }
        queryWrapper.select();
        IPage<VaccinationEntity> iPage = vaccinationService.page(page, queryWrapper);
        if (!iPage.getRecords().isEmpty()) {
            for (VaccinationEntity vaccinationEntity : iPage.getRecords()) {
                EXzqhEntity eXzqhEntity = eXzqhMapper.selectOne(Wrappers.<EXzqhEntity>lambdaQuery().eq(EXzqhEntity::getDM, vaccinationEntity.getXzqh()+"000"));
                if (eXzqhEntity != null) {
                    vaccinationEntity.setXzq(eXzqhEntity.getMC());
                }
                eXzqhEntity = eXzqhMapper.selectOne(Wrappers.<EXzqhEntity>lambdaQuery().eq(EXzqhEntity::getDM, vaccinationEntity.getTownship()));
                if (eXzqhEntity != null) {
                    vaccinationEntity.setXzc(eXzqhEntity.getMC());
                }
            }
        }
        return DataResult.success(iPage);
    }

    @ResponseBody
    @PostMapping("/vaccination/pinfobyidcard")
    public DataResult pinfobyidcard(@RequestBody Map<String, Object> params) {
        String identity = "";
        if(params.size()>0){
            identity = params.get("identity").toString();
        }

        List<VaccinationEntity> pinfobyidcard = vaccinationService.pinfobyidcard(identity);
        return DataResult.success(pinfobyidcard);
    }


}
